<template>
    <a-card :bordered="false">
        <s-table ref="table" :columns="columns" :data="loadData" :alert="options.alert.show" bordered
            :row-key="(record) => record.id" :tool-config="toolConfig" :row-selection="options.rowSelection">
            <template #operator class="table-operator">
                <a-space>
                    <a-button type="primary" @click="formRef.onOpen()">
                        <template #icon><plus-outlined /></template>
                        新增
                    </a-button>
                    <a-button danger @click="deleteBatchAppInfoTestUrl()">删除</a-button>
                </a-space>
            </template>
            <template #bodyCell="{ column, record }">
                <template v-if="column.dataIndex === 'testUrl' || column.dataIndex === 'testApp'">
                    <div style="display: flex; align-items: center">
                        <img style="width: 30px; height: 30px;" :src="setLogo(record[column.dataIndex])" alt="" />
                    </div>
                </template>
                <template v-if="column.dataIndex === 'action'">
                    <a-space>
                        <a @click="formRef.onOpen(record)">编辑</a>
                        <a-divider type="vertical" />
                        <a-popconfirm title="确定要删除吗？" @confirm="deleteAppInfoTestUrl(record)">
                            <a-button type="link" danger size="small">删除</a-button>
                        </a-popconfirm>
                    </a-space>
                </template>
            </template>
        </s-table>
    </a-card>
    <Form ref="formRef" @successful="table.refresh(true)" />
</template>

<script setup name="appinfotesturl">
import { message } from 'ant-design-vue'
const fileUrl = import.meta.env.VITE_FILE_URL
import Form from './form.vue'
import appInfoTestUrlApi from '@/api/biz/appInfoTestUrlApi'
const table = ref()
const formRef = ref()
const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
const columns = [
    {
        title: 'Android',
        dataIndex: 'testUrl'
    },
    {
        title: 'IOS',
        dataIndex: 'testApp'
    },
    {
        title: 'pc测试环境地址',
        dataIndex: 'pcTestUrl'
    },
    {
        title: '操作',
        dataIndex: 'action',
        align: 'center',
        width: '150px'
    }
]

let selectedRowKeys = ref([])
// 列表选择配置
const options = {
    alert: {
        show: false,
        clear: () => {
            selectedRowKeys = ref([])
        }
    },
    rowSelection: {
        onChange: (selectedRowKey, selectedRows) => {
            selectedRowKeys.value = selectedRowKey
        }
    }
}
// logo显示
const setLogo = (img) => {
    let str = `${fileUrl}/${JSON.parse(img)[0].url}`
    return str
}
const loadData = (parameter) => {
    return appInfoTestUrlApi.appInfoTestUrlPage(parameter).then((data) => {
        return data
    })
}
// 删除
const deleteAppInfoTestUrl = (record) => {
    let params = [
        {
            id: record.id
        }
    ]
    appInfoTestUrlApi.appInfoTestUrlDelete(params).then(() => {
        table.value.refresh(true)
    })
}
// 批量删除
const deleteBatchAppInfoTestUrl = () => {
    if (selectedRowKeys.value.length < 1) {
        message.warning('请选择一条或多条数据')
        return false
    }
    const params = selectedRowKeys.value.map((m) => {
        return {
            id: m
        }
    })
    appInfoTestUrlApi.appInfoTestUrlDelete(params).then(() => {
        table.value.clearRefreshSelected()
    })
}
</script>
